﻿using System;
using System.Collections;
using System.Windows.Forms;
using BLL_NhaSach;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;

namespace AppQlyNhaSach.Sach
{
    public partial class frmSach : DevExpress.XtraEditors.XtraForm
    {
        private SachBLL sachBLL;
        private TheLoaiBLL tlBLL;

        public frmSach()
        {
            InitializeComponent();
        }

        private void frmSach_Load(object sender, EventArgs e)
        {
			sachBLL = new SachBLL(Properties.Settings.Default.ConnectionStringUser);
			tlBLL = new TheLoaiBLL(Properties.Settings.Default.ConnectionStringUser);
            grdSach.DataSource = sachBLL.getAllData();
            loadCboTheLoai();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                String id = gridView1.GetFocusedRowCellValue("MaSach").ToString();
                DAL_NhaSach.Sach sach = (DAL_NhaSach.Sach)sachBLL.findObjects(id);
                txtMa.Text = sach.MaSach;
                txtTen.Text = sach.TenSach;
                txtTacGia.Text = sach.TacGia;
                txtGiaBia.Text = sach.GiaBia.ToString();
                txtSoLuongTon.Text = sach.SoLuongTon.ToString();
                cboTheLoai.SelectedValue = sach.TheLoai.MaLoai;
                txtTen.ToolTip = sach.TenSach;
            }
            catch { }
        }

        private void loadCboTheLoai()
        {
            cboTheLoai.DataSource = tlBLL.getAllData();
            cboTheLoai.DisplayMember = "TenLoai";
            cboTheLoai.ValueMember = "MaLoai";
            //DAL_NhaSach.Sach s = (DAL_NhaSach.Sach)sachBLL.findObjects(txtMa.Text);
            //cboTheLoai.SelectedValue = s.TheLoai.MaLoai;
        }

        public ArrayList getRowsID()
        {
            int[] lst_rows = gridView1.GetSelectedRows();
            ArrayList id_lst = new ArrayList();
            foreach (int i in lst_rows)
            {
                String id = gridView1.GetRowCellValue(i, "MaSach").ToString();
                id_lst.Add(id);
            }
            return id_lst;
        }

        /// <summary>
        /// trạng thái thêm
        /// </summary>
        /// <param name="state"></param>
        private void changeStateAdd(bool state)
        {
            if (state)
            {
                grdSach.Enabled = false;
                //btnHuy.Visible = true;
                //txtMa.Properties.ReadOnly = false;
                txtGiaBia.Properties.ReadOnly = false;
                txtTen.Properties.ReadOnly = false;
                txtTacGia.Properties.ReadOnly = false;
                txtSoLuongTon.Properties.ReadOnly = false;
                cboTheLoai.Enabled = true;
                resetView();
            }
            else
            {
                grdSach.Enabled = true;
                //btnHuy.Visible = false;
                txtMa.Properties.ReadOnly = true;
                txtGiaBia.Properties.ReadOnly = true;
                txtTen.Properties.ReadOnly = true;
                txtTacGia.Properties.ReadOnly = true;
                txtSoLuongTon.Properties.ReadOnly = true;
                cboTheLoai.Enabled = false;
                resetView();
            }
        }

        /// <summary>
        /// reset lại dữ liệu
        /// </summary>
        private void resetView()
        {
            txtMa.Text = "";
            txtTen.Text = "";
            txtTacGia.Text = "";
            txtGiaBia.Text = "0";
            txtSoLuongTon.Text = "150";
            cboTheLoai.SelectedIndex = -1;
        }
        private String generateCode(String startCode){
            Random rd = new Random();
            int code = rd.Next(100000, 10000000);
            return startCode + code.ToString();
        }
        private String getStartCode(String theloai)
        {
            String result="";
            foreach(char c in theloai){
                Boolean isUpper = Char.IsUpper(c);
                if (isUpper)
                    result += c;
            }
            return result;
        }

        private void frmSach_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMainNS.isSach = false;
        }

    }
}